package com.fourthGroup.controller;

import com.fourthGroup.model.Coupon;
import com.fourthGroup.model.UserCoupon;
import com.fourthGroup.service.CouponService;
import com.fourthGroup.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping
    public Result<Coupon> createCoupon(@RequestBody Coupon coupon) {
        log.info("创建优惠券: {}", coupon);
        return couponService.createCoupon(coupon);
    }

    @GetMapping
    public Result<List<Coupon>> listCoupons() {
        log.info("查询优惠券列表");
        return couponService.listCoupons();
    }

    @GetMapping("/{id}")
    public Result<Coupon> getCoupon(@PathVariable Long id) {
        log.info("查询优惠券详情: {}", id);
        return couponService.getCoupon(id);
    }

//    *
    @PostMapping("/grab")
    public Result<Boolean> grabCoupon(@RequestParam Integer userId, @RequestParam Long couponId) {
        log.info("用户{}抢购优惠券{}", userId, couponId);
        return couponService.grabCoupon(userId, couponId);
    }

    @PostMapping("/use")
    public Result<Boolean> useCoupon(@RequestParam Integer userId, @RequestParam Long couponId, @RequestParam String orderId) {
        log.info("用户{}使用优惠券{}，订单号: {}", userId, couponId, orderId);
        return couponService.useCoupon(userId, couponId, orderId);
    }

    @GetMapping("/user/{userId}")
    public Result<List<UserCoupon>> getUserCoupons(@PathVariable Integer userId) {
        log.info("查询用户{}的优惠券列表", userId);
        return couponService.getUserCoupons(userId);
    }

    @PostMapping("/preheat/{couponId}")
    public Result<Void> preHeatCouponStock(@PathVariable Long couponId) {
        log.info("预热优惠券{}库存", couponId);
        couponService.preHeatCouponStock(couponId);
        return Result.success();
    }
} 